package com.csx.micrometer.invoke.config;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p> what does this class do ? </p>
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2023/11/8 周三 17:38
 */
public class PrometheusCustomizer  implements MeterRegistryCustomizer<MeterRegistry> {

    @Value("${ignore.uris:/micrometer/test2}")
    private String ignoreUris;

    @Override
    public void customize(MeterRegistry registry) {
        if(StringUtils.isBlank(ignoreUris)) {
            return;
        }

        String[] split = ignoreUris.split(",");
        if(split.length == 0){
            return;
        }
        List<String> collect = Arrays.stream(split).collect(Collectors.toList());

        registry.config().meterFilter(MeterFilter.deny(id -> {
            String uri = id.getTag("uri");
            return uri != null && collect.contains(uri);
        }));
    }
}
